package com.click.app.webapp.reportObjects;

import com.click.app.model.LaundryItem;
import com.click.app.model.LaundryRequestItem;
import com.click.app.model.ModelSize;
import com.click.app.service.LaundryItemManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author Joel Chuca Condo
 * @date 05/07/2009
 */
public class LaundryRequestTable {

    private List<String> head;
    private List<BodyRow> body;
    private List<LaundryRequestItem> laundryRequestItems;
    private LaundryItemManager laundryItemManager;
    private Set<ModelSize> modelSizes;
    private boolean correct;
    

    public LaundryRequestTable(List<LaundryRequestItem> laundryRequestItems, LaundryItemManager laundryItemManager, Set<ModelSize> modelSizes) {
        this.laundryRequestItems = laundryRequestItems;
        this.laundryItemManager = laundryItemManager;
        this.modelSizes = modelSizes;
        head = new ArrayList<String>();
        body = new ArrayList<BodyRow>();
        correct = false;
        init();
    }

    public List<BodyRow> getBody() {
        return body;
    }

    public List<String> getHead() {
        return head;
    }

    public boolean isCorrect() {
        return correct;
    }

    private void init() {
        head.add("Editar");
        head.add("T. Lavado");        
        if( modelSizes.isEmpty() ){
            System.out.println("modelSizes List is empty");
            correct = false;
            return;
        }

        List<Integer> totalQuantitiesHor = new ArrayList<Integer>();
        for (ModelSize modelSize : modelSizes) {
            head.add(modelSize.getValueSize());
            totalQuantitiesHor.add(0);
        }
        head.add("Total");
        totalQuantitiesHor.add(0);
        System.out.println("size head: "+head.size());

        if (laundryRequestItems != null) {
            if (laundryRequestItems.size() > 0) {

                BodyRow row = new BodyRow();
                for (LaundryRequestItem laundryRequestItem : laundryRequestItems) {
                    row = new BodyRow();
                    row.add(laundryRequestItem.getId().toString());
                    row.add(laundryRequestItem.getLaundryType().getType());

                    List<LaundryItem> laundryItems = laundryItemManager.findByOrderSize(laundryRequestItem.getId());
                    System.out.println(laundryItems);
                    for (int i = 0; i < laundryItems.size(); i++) {
                        row.add(laundryItems.get(i).getQuantity().toString());
                        totalQuantitiesHor.set(i, totalQuantitiesHor.get(i)+laundryItems.get(i).getQuantity());
                    }
                    row.add(laundryRequestItem.getQuantity().toString());
                    System.out.println("row bosy: "+row);
                    int lastPos = laundryItems.size();
                    totalQuantitiesHor.set(lastPos, totalQuantitiesHor.get(lastPos)+laundryRequestItem.getQuantity());
                    //System.out.println("row size: "+row.size());
                    body.add(row);
                }
                row = new BodyRow();
                row.add("");
                row.add("Total");
                for (int i = 0; i < totalQuantitiesHor.size(); i++) {
                    row.add(totalQuantitiesHor.get(i).toString());
                }
                body.add(row);
                System.out.println("body size: "+body.size());
                correct = true;

            } else {
                System.out.println("Laundry Request Items is empty");
            }
        } else {
            System.out.println("Laundry Request Items is null");
            correct = false;
        }
    }

}
